package com.example.common.utils;

import com.example.sys.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

    private static Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    private static User user = new User();

    private static SecurityUtil securityUtil = null;

    public static SecurityUtil getInstance() {
        if(securityUtil == null || user.getId() == null) {
            synchronized (SecurityUtil.class) {
                if(securityUtil == null) {
                    securityUtil = new SecurityUtil();
                }
            }
        }
        return securityUtil;
    }

    public SecurityUtil() {
        BeanUtils.copyProperties(principal, user);
    }

    public String getUserId() {
        return user.getId();
    }

    public String getUserName() {
        return user.getUsername();
    }

    public String getNickname() {
        return user.getNickname();
    }

    public String getPhone() {
        return user.getPhone();
    }

    public String getToken() {
        return user.getToken();
    }
}
